<?php

// class类的用法

namespace phpStart;
header("Content-type: text/html; charset=utf-8");

class Person
{
    // 人的成员属性
    var $name; //人的名字
    var $age; //人的年龄

    //人的成员 say() 方法
    function say()
    {
        echo "我的名字叫：" . $this->name . "<br />";
        echo "我的年龄是：" . $this->age;
    }
}

//类定义结束

$p1 = new Person(); //实例化一个对象
$p1->name = "Gonn"; //给 $p1 对象属性赋值
$p1->age = 25;
$p1->say(); //调用对象中的 say()方法


/*
 *  抽象
 */

abstract class TEST1
{
    abstract public function name1();

    public function name2()
    {

    }
}

/*
 * 继承
 */

class TEST2 extends TEST1 implements TEST3
{
    public function name1()
    {

    }
}

/*
 * 接口
 */

interface TEST3
{
    public function name2();
}



echo '<br><hr><br>';





interface UserInterface
{ //定义User的接口
    function getname();
}

interface TeacherInterface
{ //teacher相关接口
    function getLengthOfService();
}

class User implements UserInterface
{ //实现UserInterface接口
    private $name;

    public function getName()
    {
        return $this->name;
    }
}

class Teacher implements TeacherInterface
{ //实现TeacherInterface接口
    private $lengthOfService = 5; // 工龄

    public function getLengthOfService()
    {
        return $this->lengthOfService;
    }
}

// 继承自User类,同时实现了TeacherInterface接口.
class GraduateStudent extends User implements TeacherInterface
{
    private $teacher;

    public function __construct()
    {
        $this->teacher = new Teacher();
    }

    public function getLengthOfService()
    {
        return $this->teacher->getLengthOfService();
    }
}

class Act
{
    //注意这里的类型提示改成了接口类型
    public static function getUserName(UserInterface $_user)
    {
        echo "Name is " . $_user->getName() . "<br>";
    }

    //这里的类型提示改成了TeacherInterface类型.
    public static function getLengthOfService(TeacherInterface $_teacher)
    {
        echo "Age is " . $_teacher->getLengthOfService() . "<br>";
    }
}

$graduateStudent = new GraduateStudent();
Act::getUserName($graduateStudent);
Act::getLengthOfService($graduateStudent);